package com.zh.freechat.domain.user.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zh.freechat.domain.user.UserStatus;
import com.zh.webcommon.common.auditor.mp.SimpleEntity;
import com.zh.webcommon.common.avatar.RetroAvatarCreate;
import com.zh.webcommon.upload.FileUploadClient;
import com.zh.webcommon.upload.FileUploadResult;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @author ZH
 * @date 16:15 2021/7/14
 */
@Slf4j
@Getter
@Setter
@TableName(value = "ch_user",autoResultMap = true)
public class ChatUser extends SimpleEntity {
    public static final Integer SECRET = 0;
    public static final Integer MALE = 1;
    public static final Integer FEMALE = 2;
    public static final String UID_PREFIX = "ch_";

    /**
     * 用户uid
     * 手动输入
     */
    @TableId(type = IdType.INPUT)
    private String uid;

    /**
     * 用户账号
     */
    private String username;
    /**
     * 密码
     */
    @JsonIgnore
    private String password;
    /**
     * 头像
     */
    private String avatarUrl;

    /**
     * 昵称
     */
    private String nickname;
    /**
     * 性别
     */
    private Integer gender;
    /**
     * 手机号
     */
    private String phone;
    /**
     * 邮箱
     */
    private String email;
    /**
     * 角色
     */
    private String role;
    /**
     * 个人简介
     */
    private String profile;


    /**
     * 状态：0未激活，1已激活
     */
    private Integer status;    //账号状态：0未激活——> 1已激活

    private Boolean isRobot;

    public ChatUser() {

    }
    public ChatUser(String id) {
        this.uid = id;
    }

    public void generateAvatar(FileUploadClient uploadClient) {
        String filename = this.getUid() + "_avatar_" + UUID.randomUUID().toString().substring(30);
        byte[] imageBytes = RetroAvatarCreate.createAvatarBytes();
        FileUploadResult uploadResult = uploadClient.upload(imageBytes, filename, "image/jpg");

        if (uploadResult.isOk()) {
            String url = uploadResult.getUrl();
            if (url.indexOf('?') > 0) url = url.substring(0, url.indexOf('?'));
            this.setAvatarUrl(url);
        } else {
            log.error("头像图片上传到minio服务器出现错误！cause: {}", uploadResult.getError());
        }
    }

    public void generateUid() {
        this.uid = UID_PREFIX + UUID.randomUUID().toString().substring(24);
    }

    public void encodePassword(PasswordEncoder passwordEncoder) {
        this.setPassword(passwordEncoder.encode(this.getPassword()));
    }

    public static ChatUser createAndInitDefault() {
        ChatUser user = new ChatUser();
        user.setRole("USER");
        user.setStatus(UserStatus.USER_INACTIVE.getCode());
        user.setGender(SECRET);
        user.setCreateTime(LocalDateTime.now());
        user.generateUid();
        return user;
    }


}
